<template>
  <div class="user-management-container">
    <el-form ref="form" label-width="200px" :model="form" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="form.nickname" placeholder="请输入昵称" />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item label="所属角色" prop="role">
        <el-select v-model="form.role" placeholder="请选择所属角色">
          <el-option
            v-for="item in options2"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          />
          <!-- @click.native="chooseone(item)" -->
        </el-select>
      </el-form-item>
      <el-form-item label="锁定状态" prop="locked">
        <el-radio v-model="form.locked" :label="0">停用</el-radio>
        <el-radio v-model="form.locked" :label="1">启用</el-radio>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入电子邮箱" />
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="form.note" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <el-button class="baocun" size="medium" type="primary" @click="save">
      保存
    </el-button>
  </div>
  <!-- 增加 -->
</template>

<script>
  /* eslint-disable*/
  import { doAdd, dojuese } from '@/api/xitongyonghu'
  import { translateTitle } from '@/utils/i18n'
  import { isPassword, isPhone } from '@/utils/validate'
  import {
    provinceAndCityData,
    regionData,
    provinceAndCityDataPlus,
    regionDataPlus,
    CodeToText,
    TextToCode,
  } from 'element-china-area-data'
  //import { number } from 'echarts'
  export default {
    name: 'search',

    data() {
      const validatePassword = (rule, value, callback) => {
        if (!isPassword(value)) {
          callback(new Error(this.translateTitle('密码不能少于6位')))
        } else {
          callback()
        }
      }
      const validatePhone = (rule, value, callback) => {
        if (!isPhone(value)) {
          callback(new Error(this.translateTitle('请输入正确的手机号')))
        } else {
          callback()
        }
      }
      const validatesurfacePlot = (rule, value, callback) => {
        console.log(this.form.slideShowUrl)
        if (this.form.slideShowUrl === '') {
          callback(new Error('请添加图片!!!!'))
        } else {
          callback()
        }
      }
      return {
        options: regionDataPlus,
        selectedOptions: [],
        token: '',
        imageUrl: '',
        value: true,
        value1: true,
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        options1: [],
        options2: [],
        options3: [],
        valueone: '',
        valuetwo: '',
        form: {
          locked: 1,
        },
        activated() {
          if (this.$refs.MaskVideo.src) this.$refs.MaskVideo.play()
        },
        deactivated() {
          if (this.$refs.MaskVideo.src) this.$refs.MaskVideo.pause()
        },

        rules: {
          username: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
          ],
          nickname: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入昵称',
            },
          ],
          locked: [
            { required: true, trigger: 'change', message: '请选择状态' },
          ],
          slideShowUrl: [
            { required: true, trigger: 'change', message: '请添加图片' },
            { validator: validatesurfacePlot, trigger: 'change' },
          ],
          enterpriseName: [
            { required: true, message: '请输入企业名称', trigger: 'blur' },
          ],
          phone: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入手机号',
            },
            { validator: validatePhone, trigger: 'blur' },
          ],
          password: [
            {
              required: true,
              trigger: 'blur',
              message: '请输入密码',
            },
            { validator: validatePassword, trigger: 'blur' },
          ],
          email: [{ required: true, trigger: 'blur', message: '请输邮箱' }],

          role: [
            { required: true, trigger: 'change', message: '请选择所属角色' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.token = this.$store.getters['user/token']
      this.fetchDatatwo()
    },
    methods: {
      translateTitle,
      handleAvatarSuccess(res, file, fileList) {
        this.imageUrl = URL.createObjectURL(file.raw)
        console.log(res)
        this.form.surfacePlot = res.data
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return true
      },
      //选择区域

      handleAvatarSuccess(res, file, fileList) {
        this.imageUrl = URL.createObjectURL(file.raw)
        console.log(res)
        this.form.slideShowUrl = res.data
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return true
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.imageUrl = row.slideShowUrl
        }
        this.dialogFormVisible = true
      },
      close() {
        let userId = this.form.userId
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        if (!this.form.id) {
          this.form.userId = userId
        }
        console.log(this.form)
        this.dialogFormVisible = false
        this.imageUrl = ''
        // this.form = ''
      },
      async fetchDatatwo() {
        let queryForm = {}
        const res = await dojuese(queryForm)
        this.options2 = res.data
        console.log(res)
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              const msg = doEdit(this.form)
              // console.log(msg)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              this.$emit('fetch-data')
              this.close()
            } else {
              const msg = doAdd(this.form)

              this.$emit('fetch-data')
              this.close()
              msg.then((res) => {
                if (res.msg === '添加用户成功') {
                  this.$baseMessage(
                    res.msg,
                    'success',
                    'vab-hey-message-success'
                  )
                  this.$router.push({
                    path: '/setting/systemuser/systemuserlist',
                  })
                }
              })
            }
          }
        })
      },
    },
  }
</script>
<style>
  /* .huakuai {
    position: relative;
    left: 96%;
  } */
  .avatar-uploader .el-upload {
    position: relative;
    overflow: hidden;
    cursor: pointer;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    width: 178px;
    height: 178px;
    font-size: 28px;
    line-height: 178px;
    color: #8c939d;
    text-align: center;
  }
  .avatar {
    display: block;
    width: 178px;
    height: 178px;
  }
  .baocun {
    width: 100%;
    height: 100%;
  }
</style>
